package com.ruoyi.web.controller.course;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.course.domain.SysTopic;
import com.ruoyi.course.service.ISysTopicService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 题目Controller
 * 
 * @author YJF
 */
@RestController
@RequestMapping("/course/topic")
public class SysTopicController extends BaseController
{
    @Autowired
    private ISysTopicService sysTopicService;

    /**
     * 查询题目列表
     */
    @PreAuthorize("@ss.hasPermi('course:topic:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysTopic sysTopic)
    {
        startPage();
        List<SysTopic> list = sysTopicService.selectSysTopicList(sysTopic);
        return getDataTable(list);
    }

    /**
     * 查询全部题目列表
     */
    @PreAuthorize("@ss.hasPermi('course:topic:list')")
    @GetMapping("/wholeTopic")
    public AjaxResult wholeTopic(SysTopic sysTopic)
    {
        return AjaxResult.success(sysTopicService.selectSysTopicList(sysTopic));
    }

    /**
     * 导入
     * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @Log(title = "题目管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('course:topic:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<SysTopic> util = new ExcelUtil<SysTopic>(SysTopic.class);
        List<SysTopic> topicList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = sysTopicService.importTopic(topicList, operName);
        return AjaxResult.success(message);
    }

    /**
     * 模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<SysTopic> util = new ExcelUtil<SysTopic>(SysTopic.class);
        util.importTemplateExcel(response, "题目数据");
    }

    /**
     * 导出题目列表
     */
    @PreAuthorize("@ss.hasPermi('course:topic:export')")
    @Log(title = "题目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysTopic sysTopic)
    {
        List<SysTopic> list = sysTopicService.selectSysTopicList(sysTopic);
        ExcelUtil<SysTopic> util = new ExcelUtil<SysTopic>(SysTopic.class);
        util.exportExcel(response, list, "题目数据");
    }

    /**
     * 获取题目详细信息
     */
    @PreAuthorize("@ss.hasPermi('course:topic:query')")
    @GetMapping(value = "/{topicId}")
    public AjaxResult getInfo(@PathVariable("topicId") Long topicId)
    {
        return AjaxResult.success(sysTopicService.selectSysTopicByTopicId(topicId));
    }

    /**
     * 新增题目
     */
    @PreAuthorize("@ss.hasPermi('course:topic:add')")
    @Log(title = "题目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysTopic sysTopic)
    {
        return toAjax(sysTopicService.insertSysTopic(sysTopic));
    }

    /**
     * 修改题目
     */
    @PreAuthorize("@ss.hasPermi('course:topic:edit')")
    @Log(title = "题目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysTopic sysTopic)
    {
        return toAjax(sysTopicService.updateSysTopic(sysTopic));
    }

    /**
     * 删除题目
     */
    @PreAuthorize("@ss.hasPermi('course:topic:remove')")
    @Log(title = "题目", businessType = BusinessType.DELETE)
	@DeleteMapping("/{topicIds}")
    public AjaxResult remove(@PathVariable Long[] topicIds)
    {
        return toAjax(sysTopicService.deleteSysTopicByTopicIds(topicIds));
    }
}
